from flask import Flask,request,render_template,jsonify
from apscheduler.schedulers.blocking import BlockingScheduler
from jieba.analyse import extract_tags
import string
import json
import utils
import spider


#定时爬虫
def my_job():
    spider.update_details()
sched = BlockingScheduler()
sched.add_job(my_job, 'interval', minutes=5)

app = Flask(__name__)

@app.route('/')
def hello_world():
    return render_template("main.html")

#热搜
@app.route('/hotSearch')
def get_r2_data():
    data = utils.get_hotSearch_data()
    d = []
    for i in data:
        k = i[0].rstrip(string.digits)
        v = i[0][len(k):]
        ks = extract_tags(k)
        for j in ks:
            if not j.isdigit():
                d.append({"name": j,"value": v})
    return jsonify({"kws": d})

# 趋势图
@app.route("/trend")
def get_trend_data():
    data = utils.get_trend_data()
    day, confirm_add, suspect_add = [], [], []
    for a, b, c in data:
        day.append(a.strftime("%m-%d"))  # a是datatime类型
        confirm_add.append(b)
        suspect_add.append(c)
    return jsonify({"day": day, "confirm_add": confirm_add, "suspect_add": suspect_add})

# 累计数量等
@app.route("/summary")
def get_summary_data():
    data, data_new = utils.get_summary_data()
    return jsonify({"confirm":int(data[0]),"confirm_now":int(data[1]),"heal":int(data[2]),"dead":int(data[3]),
                    "confirm_new":int(data_new[0]),"domestic":int(data_new[1]),"abroad":int(data_new[2]),"dead_new":int(data_new[3])})

# 分布图
@app.route("/map")
def get_map_data():
    res = []
    for tup in utils.get_map_data():
        res.append({"name":tup[0],"value":int(tup[1])})
    return jsonify({"data":res})

#省市下转
@app.route("/province")
def get_provinceMap_data():
    res = []
    for tup in utils.get_province_data():
        res.append({"name": tup[0] + "市", "value": int(tup[1])})
    return jsonify({"mydata": res})

# pie图
@app.route("/pie")
def get_pie_data():
    res = []
    for tup in utils.get_pie_data():
        res.append({"value": int(tup[1]), "name": tup[0]})
    return jsonify({"mydata": res})

# top图
@app.route("/top")
def get_top_data():
    data = utils.get_top_data()
    city = []
    confirm = []
    for k,v in data:
        city.append(k)
        confirm.append(int(v))
    return jsonify({"city": city, "confirm": confirm})

# 风险播报
@app.route("/risk")
def get_risk_data():
    data = utils.get_risk_data()
    # end_update_time, province, city, county, address, type
    details = []
    risk = []
    end_update_time = data[0][0]
    for a,b,c,d,e,f in data:
        risk.append(f)
        details.append(f"{b}\t{c}\t{d}\t{e}")
    return jsonify({"update_time": end_update_time, "details": details, "risk": risk})

# 跳转自我检测
@app.route("/check", methods=['GET', 'POST'])
def check():
    return render_template("check.html")

# 自我检测
@app.route("/self_check", methods=['GET', 'POST'])
def self_check():
    uname = request.form.get('uname')
    usex = request.form.get('usex')
    uage = request.form.get('uage')
    uphone = request.form.get('uphone')
    uaddress = request.form.get('uaddress')
    problem = request.form.get('problem')
    op1 = request.form.get('inlineRadioOptions1')
    op2 = request.form.get('inlineRadioOptions2')
    op3 = request.form.get('inlineRadioOptions3')
    predict = ""
    if op1 == 'option1' or op2 == 'option1' or op3 == 'option1':
        predict = "新冠风险高,请尽快与当地工作人员联系"
    if op1 == 'option3' and op1 == 'option3' and op1 == 'option3':
        predict = "新冠风险中"
    if op1 == 'option2' and op2 == 'option2' and op3 == 'option2':
        predict = "新冠风险小"
    if predict == "":
        predict = "信息不完善"
    print(uname, usex, uage, uphone, uaddress, problem)
    utils.self_check(uname, usex, uage, uphone, uaddress, problem, predict)
    return render_template("check.html", res = predict)

#谣言
@app.route("/rumor")
def get_rumor_data():
    result = utils.get_rumor_data()
    return render_template("rumor.html", res = result)

#谣言搜索
@app.route("/search_rumor", methods=['GET', 'POST'])
def search_rumor():
    key = request.form.get('key')
    result = utils.search_rumor_data(key)
    return render_template("rumor.html", res = result)

#往期视频
@app.route('/video')
def video():
    return render_template('movie.html')
@app.route('/movie')
def index():
    filePath = 'static/movies'
    a = utils.get_video_data(filePath)
    res = []
    for i in a:
        res.append('../' + filePath + '/' + i)
    return jsonify({"re" : res})

#跳转举报
@app.route('/toreport', methods=['GET', 'POST'])
def toreport():
    return render_template("report.html")
#举报
@app.route('/report', methods=['GET', 'POST'])
def report():
    uname = request.form.get('uname')
    usex = request.form.get('usex')
    uage = request.form.get('uage')
    uphone = request.form.get('uphone')
    uaddress = request.form.get('uaddress')
    patient = request.form.get('patient')
    paddress = request.form.get('paddress')
    problem = request.form.get('problem')
    utils.report_data(uname, usex, uage, uphone, uaddress, problem, patient, paddress)
    return render_template("report.html")

#科普知识
@app.route('/knowledge')
def knowledge():
    return render_template('knowledge.html')

#疫情资讯
@app.route('/info')
def cov_info():
    return render_template('info.html')

#风险区搜索
@app.route("/search_risk", methods=['GET'])
def search_risk():
    key = request.args.get('key')
    data = utils.search_risk_data(key)
    details = []
    risk = []
    end_update_time = data[0][0]
    for a, b, c, d, e, f in data:
        risk.append(f)
        details.append(f"{b}\t{c}\t{d}\t{e}")
    return jsonify({"update_time": end_update_time, "details": details, "risk": risk})

if __name__ == '__main__':
    app.run(host="0.0.0.0",port=9999)
